<template>
	<zh-form ref="paramsForm" :model="state.form" style="margin-top: 20px">
    <zh-form-date prop="periodYear" label="所属年度" type="year" v-model="state.form.periodYear"></zh-form-date>
		<zh-form-select
			prop="operating"
			v-model="state.form.operating"
			label="操作期规则"
			:options="state.dictData.operating"
		></zh-form-select>
    <zh-form-date label="业务年月" prop="ym" type="month" v-model="state.form.ym" format="YYYYMM"></zh-form-date>
    <zh-form-date label="开始时间" prop="begTime" type="datetime" v-model="state.form.begTime" format="YYYY-MM-DD HH:mm:ss"></zh-form-date>
    <zh-form-date label="结束时间" prop="endTime" type="datetime" v-model="state.form.endTime" format="YYYY-MM-DD HH:mm:ss"></zh-form-date>
    <zh-form-cascader
					prop="areaCode"
					label="行政区划"
          :props="{checkStrictly: true,emitPath:false}"
					placeholder="请选择"
					v-model="orgInfo.areaCode"
					:options="state.areaCodeSelectTreeData"
				></zh-form-cascader>
    <zh-form-select placeholder="请搜索，单选"
          funcCode="org0001"
          label="所在单位组织"
          prop="orgId"
          searchField="orgName"
          :fieldNames="{
            label: 'orgName',
            value: 'orgId',
          }"
          :lazy="true"
          v-model="state.form.orgId">
    </zh-form-select>
		<zh-form-select
					prop="appId"
					:lazy="true"
					label="所属应用"
					v-model="state.form.appId"
					:fieldNames="{ label: 'appName', value: 'appId' }"
					funcCode="application0012"
					searchField="appName"
		></zh-form-select>
    <zh-form-input prop="messagePrompts" type="textarea" :span="24" label="开始时间前提示" v-model="state.form.messagePrompts"></zh-form-input>
    <zh-form-input prop="messageTip" type="textarea" :span="24" label="结束时间后提示" v-model="state.form.messageTip"></zh-form-input>
		<zh-form-input prop="remarks" type="textarea" :span="24" label="备注" v-model="state.form.remarks"></zh-form-input>
	</zh-form>
</template>

<script lang="ts" setup name="SystemParamsForm">
//----------------定义接收参数-------------------------------
const props = defineProps({
	periodInfo: {
		type: Object,
		default: () => {},
		required: false,
	},
	modelValue: Boolean,
});
const emit = defineEmits(['update:modelValue']);

//------------------定义子组件---------------------------
const paramsForm = ref<any>({});

//------------------内部变量定义-------------------------
const state = reactive({
	dictData: {} as any,
	areaCodeSelectTreeData: [],
	form: {} as any,
	initData: {} as any,
});

//------------------定义调用方法-------------------------
function getAreaCodeData() {
	setTimeout(() => {
		$api.common.send.post('dict0026', {}).then((res) => {
			state.areaCodeSelectTreeData = res.result;
		});
	}, 20);
}
function saveParams() {
	// 表单验证
	return new Promise((resolve, inject) => {
		paramsForm.value.validate((valid) => {
			if (valid) {
				const systemParams: any = state.form;
				if (systemParams.sysId) {
					setTimeout(() => {
						$api.common.send
							.post('sysCommon0006', systemParams)
							.then(() => {
								resolve(true);
								emit('update:modelValue', false);
								$messageBox.success('修改成功', '成功提示');
							})
							.catch(() => {
								inject();
							});
					}, 20);
				} else {
					setTimeout(() => {
						$api.common.send
							.post('sysCommon0005', systemParams)
							.then(() => {
								$messageBox.success('新增成功', '成功提示');
								resolve(false);
								emit('update:modelValue', false);
							})
							.catch(() => {
								inject();
							});
					}, 20);
				}
			}
		});
	});
}

//------------------初始化------------------------------
if (props.paramsInfo) {
	state.initData = Object.assign({}, props.paramsInfo);
	state.form = state.initData;
} else {
	state.initData = {};
}
const dictParams = {
	dictFields: 'operating',
};
$api.common.getBatchDict.post(dictParams).then((res) => {
	state.dictData = res.result;
});
getAreaCodeData()
defineExpose({ saveParams });
</script>
